<template>
	<view>
		<u-subsection :list="catelist" :current="current" @change="sectionChange"></u-subsection>
		<view class="list">
			<block v-for="(item,index) in list" :key="index">
				<view class="orderDetail">
					<view class="top">
						<text class="status tag-danger" v-if="!item.receive_time">
							待配送
						</text>
						<text class="status tag-success" v-else>
							已配送
						</text>
						<text class="order_no">订单号：{{item.order_no}}</text>
					</view>
					<view class="goodsinfo">
						<view class="m-img" :style="'background:url('+parseImg(item.goods.img)+')no-repeat;background-size:cover;'">
						</view>
						<view class="m-text">
							<view class="m-title">
								{{item.goods.name}}
							</view>
							<view class="m-descripe">
								{{item.goods.des}}
							</view>
							<view class="m-price">
								￥{{item.price}} * {{item.number}}{{item.spec.unit}}
								<text style="margin-left: 10rpx;" class="tag-danger" v-if="item.refuse_number">退{{item.refuse_number}}{{item.spec.unit}}</text>
							</view>
						</view>
					</view>					
					<view class="footer">
						<view style="margin-bottom: 10rpx;">下单日期：{{item.createtime}}</view>
						<view v-if="item.receive_time" style="margin-bottom: 10rpx;">配送日期：{{item.receive_time | parseTime}}</view>
						<view class="zhifu"><text>支付方式：{{item.pay_type}}</text><text>合计金额：￥{{item.total_money}}</text></view>
					</view>
					<view class="btn-group">
						<u-button customStyle="margin-right:10rpx;" v-if="!item.receive_time" size="small" type="primary" text="联系司机" @click="contact(item.gird_id)"></u-button>
						<u-button  v-if="!item.receive_time" size="small" type="error" text="取消订单" @click="cancel(item,index)"></u-button>
					</view>
				</view>
			</block>	
			<view v-if="list.length==0">
				<u-empty text="没有订单!" mode="search" marginTop="50"></u-empty>
			</view>
		</view>
	</view>
</template>

<script>
	import {methods} from '@/utils/methods.js';
	export default {
		data() {
			return {
				customer:'',
				catelist:['全部','待配送','已配送','有退货'],
				current:0,
				endData:false,
				page:1,
				list:[],		
			}
		},
		created() {
			this.customer=this.$customer();
			if(!this.customer){
				this.$reLaunch('index/index');
				return;
			}
			this.init();
		},
		filters:{
			parseTime:function(data){
				if(data){
					return data.substr(0,16);
				}
			}	
		},
		onPullDownRefresh() {
			this.init(true);
		},
		methods: {
			...methods,
			init:function(pulldown){
				this.page=1;
				this.endData=false;
				this.list=[];
				this.pagelist(pulldown);
			},
			contact:function(gird_id){
				this.$get('buy/contactDriver',{gird_id:gird_id}).then(res=>{
					uni.showModal({
						title:'联系司机，'+res.realname,
						content:'你确定要拨打电话给司机吗？',
						success(t) {
							if(t.confirm){
								uni.makePhoneCall({
									phoneNumber:res.mobile
								});
							}
						}
					});
				});
			},
			cancel:function(item,index){
				uni.showModal({
					title:'提醒',
					content:'您确定要取消该订单吗？',
					success: (e) => {
						if(e.confirm){
							this.$post('buy/cancel',{order_no:item.order_no},true).then(res=>{
								this.list.splice(index,1);
							});
						}
					}
				});
			},
			parseImg:function(e){
				if(!e){
					return baseUrl+'assets/img/logo.png'
				}
				let arr=e.split(',');
				return arr[0];
			},
			sectionChange:function(i){
				this.current=i;
				this.init();
			},
			pagelist:function(pulldown=false){
				if(this.endData){
					this.$msg('已经到底了');
					return;
				}
				this.$get("buy/orderlist",{
					customer_id:this.customer.id,
					order_type:this.current,
					page:this.page
				},true).then(res=>{
					if(pulldown){
						uni.stopPullDownRefresh();
					}
					if(res.length<10){
						this.endData=true;
					}
					this.list=this.list.concat(res);	
					this.page++;	
				});		
			}
		}
	}
</script>

<style lang="scss">
.list{
	padding: 20rpx;
	font-size: 24rpx;
	.orderDetail{
		min-height: 100rpx;
		margin-bottom: 20rpx;
		border-radius: 6rpx;
		box-shadow: 0rpx 0rpx 20rpx #bebebe;
		padding: 20rpx;
		.top{
			display: flex;
			justify-content: space-between;
		}
		.footer{
			text-align: left;
			margin-top: 20rpx;
			.zhifu{
				display: flex;
				justify-content: space-between;
				margin-top: 10rpx;
			}
		}
		.btn-group{
			margin-top: 20rpx;
			display: flex;
			justify-content: flex-end;
			margin-left: 80%;
			width: 20%;			
		}
		.goodsinfo{
			display: flex;
			flex-direction: row;
			width: 100%;
			justify-content: space-between;
			align-items: flex-end;
			margin-top: 20rpx;
			background-color: #f7f7f7;
			padding:10rpx 0;
			.m-img{
				height: 190upx;
				width: 210upx;
				border-radius: 10upx;
				margin-left: 10rpx;
			}
			.m-text{
				flex:1;
				padding: 0 20upx;
				.m-title{
					font-size: 32upx;
					color:#4c4c4c;
					margin-bottom: 20upx;
				}
				.m-descripe{
					font-size: 24upx;
					color:#999999;
					display: flex;
					justify-content: space-between;
					margin-bottom: 20upx;
				}
				.m-price{
					font-size: 28upx;
					color:#ff582b;
					font-weight: bold;
				}
			}
		}
	}
}
</style>
